package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.KeywordDao;
import com.jbp.exception.RRException;
import com.jbp.model.Keyword;
import com.jbp.service.KeywordService;
import com.jbp.service.PropertyService;
import com.jbp.utils.qiniuyun.Qiniuyun;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;

import java.util.List;
import java.util.Map;

/**
 * (Keyword)表服务实现类
 * @author cc
 * @since 2020-10-29 11:45:08
 */
@Service
public class KeywordServiceImpl extends ServiceImpl<KeywordDao, Keyword> implements KeywordService {
    @Autowired
    private PropertyService propertyService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<Keyword> queryWrapper = new QueryWrapper<>();
        if (params.get("id") != null){
            queryWrapper.like("id",params.get("id"));
        }
        IPage<Keyword> page = this.page(
                new Query<Keyword>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**关键字屏蔽*/
    @Override
    public String replaceKeyWord(String src) {
        if (StringUtils.isEmpty(src)) {
            return src;
        }

        String replace = propertyService.getV("keword_replace");

        List<Keyword> keywords = list();
        for (Keyword keyword : keywords) {
            src = src.replaceAll(keyword.getWord(), replace);
        }
        return src;
    }

    public static void main(String[] args) {
        String s="砸蛋咯";
    }

    /**关键字检测*/
    @Override
    public boolean containsKeyWord(String src) {
        if (StringUtils.isEmpty(src)) {
            return false;
        }

        List<Keyword> keywords = list();
        for (Keyword keyword : keywords) {
            if (src.contains(keyword.getWord())) {
                return true;
            }
        }
        return false;
    }

    /**文本合规检测*/
    @Override
    public void checkContent(String content){
        if (StringUtils.isBlank(content)) {
            return;
        }
        if (containsKeyWord(content) || !Qiniuyun.check(content)) {
            throw new RRException("内容违规");
        }
    }
}